'use strict';
// Copyright 2014 Blueant Inc. All Rights Reserved.

/**
 * @fileoverview 查询累计充值天数活动信息 
 * @author agl
 */

var _ = require('lodash');
var async = require('async');
var Code = require('../../../../consts/code');
var PlayerManager = require('../../../../libs/playerManager');
var dataApi = require('../../../../libs/dataApi.js');
var configUtil = require('../../../../util/configUtil.js');
var constantUtil = require('../../../../util/constantUtil.js');

module.exports = function (msg, session, next) {
    var role_id = session.uid;

    var pay_list = [];
    var total_pay_day = 0;

    var player_manager = new PlayerManager(role_id);
    var start_time = configUtil.getOpenGameTime();
    var end_time = start_time + constantUtil.value('open_service_time') * configUtil.second_per_day;
    async.series(
        [
            function (cb) {
                player_manager.refreshData(cb);
            },
            function (cb) {
                player_manager.getModel(['role', 'accrued'], cb);
            },
            function (cb) {
                var role_model = player_manager.role;
                var pick_list = role_model.total_pay_day_picked;
                total_pay_day = role_model.total_pay_day;
                dataApi.cumulate_day.each(function (pay_info, key) {
                    var day = parseInt(key);
                    var status = 0;
                    if (total_pay_day >= day) {
                        status = 1;
                        if (_.indexOf(pick_list, day) >= 0) {
                            status = 2;
                        }
                    }
                    pay_list.push({
                        name: pay_info[0].name,
                        day: day,
                        status: status
                    });
                });

                cb();
            }
        ],
        function (err) {
            if (!!err) {
                console.error(err);
                return next(null, {
                    code: Code.FAIL
                });
            }
            return next(null, {
                code: Code.OK,
                result: {
                    start_time: start_time,
                    end_time: end_time,
                    total_pay_day: total_pay_day,
                    pay_list: pay_list
                }
            });
        }
    );
};
var end_time;
